/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import org.apache.commons.collections.Factory;

import com.toedter.calendar.*;
import com.beans.Projets;
import com.beans.Prospects;
import com.beans.Sorties;
import com.beans.Users;
import com.metier.ProjetMetier;
import com.metier.ProspectsMetier;
import com.metier.SortiesMetier;

/**
 * 
 * @author Mamadou
 */

/**
 * 
 * @author Mamadou
 */
public class PanelSorties extends javax.swing.JPanel implements MouseListener {

	private javax.swing.JButton boutonAjouter;
	private javax.swing.JButton boutonAnnulerAjoutSortie;
	private javax.swing.JButton boutonImprimer;
	private javax.swing.JButton boutonModifier;
	private javax.swing.JButton boutonRechercher;
	private javax.swing.JButton boutonSupprimer, boutonDetails;
	private javax.swing.JLabel jLabel1, textLabel;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JPanel panelBoutons, panelBouton1;
	private javax.swing.JPanel panelbas;
	private javax.swing.JPanel panelhaut;
	private JTextField textRechercher;
	private JLabel labelDateDebut, labelDateFin;
	private com.toedter.calendar.JDateChooser dateDebutProjet;
	private com.toedter.calendar.JDateChooser dateFinProjet;
	DefaultTableModel dtm;
	private SortiesMetier sortieMetier;
	private JTable tableListeSorties;
	private Sorties sortieToModif;

	// End of variables declaration//GEN-END:variables

	public PanelSorties() {

		sortieMetier = new SortiesMetier();
		initComponents();
		
		getTableListeSorties().addMouseListener(this);
		boutonSupprimer.setEnabled(false);
		boutonModifier.setEnabled(false);

	}

	private void initComponents() {

		panelhaut = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		panelbas = new javax.swing.JPanel();
		panelbas.setLayout(new GridLayout(1, 1));
		panelBoutons = new javax.swing.JPanel();
		panelBouton1 = new JPanel();
		panelBouton1.setBackground(new java.awt.Color(128, 130, 133));
		labelDateDebut = new JLabel("DATE DEBUT");
		labelDateFin = new JLabel("DATE FIN");

		boutonAjouter = new javax.swing.JButton();
		boutonSupprimer = new javax.swing.JButton();
		boutonModifier = new javax.swing.JButton();
		boutonImprimer = new javax.swing.JButton();
		boutonImprimer.setVisible(false);
		boutonRechercher = new javax.swing.JButton();
		boutonAnnulerAjoutSortie = new javax.swing.JButton("Annuler");
		boutonAnnulerAjoutSortie.setFont(new java.awt.Font("Verdana", 3, 14)); 
		boutonDetails = new JButton();
		jLabel2 = new javax.swing.JLabel();
		textLabel = new JLabel("");
		textRechercher = new JTextField();
		dateDebutProjet = new com.toedter.calendar.JDateChooser();
		dateFinProjet = new com.toedter.calendar.JDateChooser();
		labelDateDebut.setFont(new java.awt.Font("Verdana", 3, 12));
		labelDateFin.setFont(new java.awt.Font("Verdana", 3, 12));
		labelDateDebut.setForeground(new java.awt.Color(255, 255, 255));
		labelDateFin.setForeground(new java.awt.Color(255, 255, 255));

		setBackground(new java.awt.Color(223, 242, 255));
		setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1, 1,
				new java.awt.Color(102, 153, 255)));
		setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		setMaximumSize(new java.awt.Dimension(1050, 550));
		setMinimumSize(new java.awt.Dimension(1050, 550));
		setLayout(new java.awt.BorderLayout());
		panelBoutons.setLayout(new BorderLayout());
		boutonDetails.setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		boutonDetails.setText("DETAILS");
		boutonDetails.setVisible(false);
		boutonDetails.setBackground(new java.awt.Color(223, 242, 255));
		panelBoutons.setBackground(new java.awt.Color(128, 130, 133));
		panelhaut.setBackground(new java.awt.Color(128, 130, 133));
		panelhaut.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1,
				1, 1, new java.awt.Color(192, 36, 91)));
		panelhaut.setPreferredSize(new java.awt.Dimension(1050, 75));

		jLabel1.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/GESTION DES SORTIES.jpg")); // NOI18N

		panelbas.setBackground(new java.awt.Color(223, 242, 255));
		panelbas.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1,
				1, new java.awt.Color(102, 153, 255)));
		panelbas.setMaximumSize(new java.awt.Dimension(1050, 375));
		panelbas.setMinimumSize(new java.awt.Dimension(1050, 375));
		panelbas.setPreferredSize(new java.awt.Dimension(1056, 410));

		panelBoutons.setPreferredSize(new java.awt.Dimension(742, 35));
		panelBoutons.setBorder(javax.swing.BorderFactory.createMatteBorder(1,
				1, 1, 1, new java.awt.Color(191, 36, 94)));
		panelBoutons.setMaximumSize(new java.awt.Dimension(1050, 50));
		panelBoutons.setMinimumSize(new java.awt.Dimension(1050, 50));
		panelBoutons.setPreferredSize(new java.awt.Dimension(1050, 40));

		boutonAjouter.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/add16.png")); // NOI18N
		boutonAjouter.setToolTipText("Ajouter");

		boutonSupprimer.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/supprimer.gif"));
		boutonSupprimer.setToolTipText("Supprimer");
		boutonSupprimer.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});
		boutonAnnulerAjoutSortie.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

				// TODO Auto-generated method stub
				boutonImprimer.setVisible(false);
				boutonAjouter.setVisible(true);
				boutonModifier.setVisible(true);
				boutonRechercher.setVisible(true);
				boutonSupprimer.setVisible(true);
				boutonDetails.setVisible(true);
				textRechercher.setVisible(true);
				labelDateDebut.setVisible(true);
				labelDateFin.setVisible(true);
				dateDebutProjet.setVisible(true);
				dateFinProjet.setVisible(true);
				panelbas.removeAll();
				panelbas.add(new JScrollPane(tableListeSorties),
						BorderLayout.CENTER);
				validate();
			}
		});
		boutonModifier.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/modifier.gif"));
		boutonModifier.setToolTipText("Modifier");

		boutonImprimer.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/imprimer.gif"));
		boutonImprimer.setToolTipText("Imprimer");

		boutonRechercher.setIcon(new javax.swing.ImageIcon(
				"src/main/java/com/photos/recherchermod.png"));
		boutonRechercher.setToolTipText("Rechercher");

		jLabel2.setFont(new java.awt.Font("Verdana", 3, 14));

		panelhaut.add(jLabel1);

		// jLabel2.setText("LISTE DES PROJETS");
		textRechercher.setPreferredSize(new Dimension(100, 27));
		textRechercher.setText("Référence");

		panelBouton1.add(boutonAjouter);
		panelBouton1.add(boutonSupprimer);
		panelBouton1.add(boutonModifier);

		panelBouton1.add(boutonImprimer);
		panelBouton1.add(boutonRechercher);
		panelBouton1.add(textRechercher);
		panelBouton1.add(labelDateDebut);
		panelBouton1.add(dateDebutProjet);
		panelBouton1.add(labelDateFin);
		panelBouton1.add(dateFinProjet);
		panelBouton1.add(boutonDetails, BorderLayout.EAST);
		// panelBouton1.add(textLabel);
		// panelBouton1.add(jLabel2, BorderLayout.CENTER);
		panelBoutons.add(panelBouton1, BorderLayout.WEST);

		add(panelBoutons, java.awt.BorderLayout.CENTER); // on fait appel à
															// cette méthode
															// pour le
		add(panelhaut, BorderLayout.NORTH);
		tableauListeSories();
		panelbas.add(new JScrollPane(tableListeSorties), BorderLayout.CENTER);
		add(panelbas, BorderLayout.SOUTH);
		boutonSupprimer.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				testDeleteSortie();

			}
		});
		boutonModifier.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				chargeSotieSelectionner();
			}
		});
		boutonAjouter.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				jLabel2.setVisible(false);
				boutonImprimer.setVisible(false);
				boutonModifier.setVisible(false);
				boutonRechercher.setVisible(false);
				boutonSupprimer.setVisible(false);
				textRechercher.setVisible(false);
				labelDateDebut.setVisible(false);
				labelDateFin.setVisible(false);
				dateDebutProjet.setVisible(false);
				dateFinProjet.setVisible(false);
				boutonDetails.setVisible(false);
				boutonAjouter.setVisible(false);
                 panelbas.removeAll();
				PanelAjouterSorties panAjouterSorties = new PanelAjouterSorties(
						boutonAnnulerAjoutSortie);
				panelbas.add(panAjouterSorties, BorderLayout.SOUTH);
				validate();

			}
		});
	}

	public JTextField getTextRechercher() {
		return textRechercher;
	}

	public void setTextRechercher(JTextField textRechercher) {
		this.textRechercher = textRechercher;
	}

	// methode qui permet de charger la liste des projets dans un JTable

	public void tableauListeSories() {

		String[] entetes = { "Numero", "Referencee", "Type", "Montant",
				"Date de Création" };

		List<Sorties> listSorties = sortieMetier.chargerAllSorties();
		int nombreLigne = listSorties.size();
		System.out.println("taille de la liste" + listSorties.size());
		Object data[][] = new Object[nombreLigne][5];

		// On parcoure la liste pour recuperer les projets et remplir le tableau
		// d'objet
		for (int i = 0; i < nombreLigne; i++) {
			Sorties sorties = new Sorties();

			sorties = listSorties.get(i);

			data[i][0] = sorties.getSortNum();

			data[i][1] = sorties.getSortRef();
			data[i][2] = sorties.getSortType();
			String montant = new DecimalFormat("0").format(sorties
					.getSortMontant());// renvoie un String
			data[i][3] = montant;

			// data[i][3] = sorties.getSortMontant();

			data[i][4] = sorties.getSortDateCreate();
		}
		dtm = new DefaultTableModel(data, entetes);
		tableListeSorties = new JTable(dtm);

	}

	// methode pour la suppression d'une sortie

	public void testDeleteSortie() {

		// on recupére le nombre de ligne selectionné pour la suppression

		final int[] selection = getTableListeSorties().getSelectedRows();
		System.out.println("voici la taille selectionné " + selection.length);
		// test pour voir si la selection est vide
		if (selection.length == 0) {
			JOptionPane.showMessageDialog(null,
					"veuillez selectionner une sortie d'abord!!!!");

		} else {// si la selection n'est pas vide
			int taille = selection.length;

			for (int i = 0; i < taille; i++) {
				int index = selection[i];

				String sortieReference = (String) getTableListeSorties()
						.getValueAt(index, 1);

				Sorties sortieSupprimer = sortieMetier
						.findSortieByReference(sortieReference);
				sortieMetier.testDeleteSortie(sortieSupprimer);
				dtm.removeRow(index);

			}

		}
	}

	public JTable getTableListeSorties() {
		return tableListeSorties;
	}

	public void setTableListeSorties(JTable tableListeSorties) {
		this.tableListeSorties = tableListeSorties;
	}

	// méthode pour la mise à jour d'une sortie
	public void chargeSotieSelectionner() {

		int ligneSelectionner = getTableListeSorties().getSelectedRow();
		if (ligneSelectionner == -1) {
			JOptionPane.showMessageDialog(null,
					"veuillez selectionner une sortie  d'abord!!!!");

		} else {

			SortiesMetier sortieMetier = new SortiesMetier();
			Sorties sortieToModify = new Sorties();
			String numeroRecuperer = (String) getTableListeSorties()
					.getValueAt(ligneSelectionner, 0);
			String referenceRecuperer = (String) getTableListeSorties()

			.getValueAt(ligneSelectionner, 1);
			// String typeRecuperer = (String) getTableListeSorties()
			// .getValueAt(ligneSelectionner, 2);
			String montantrecuperer = (String) getTableListeSorties()
					.getValueAt(ligneSelectionner, 3);
			// Double montant = Double.parseDouble(montantrecuperer);
			Date dateCreationrecuperer = (Date) getTableListeSorties()
					.getValueAt(ligneSelectionner, 4);
			// On charge les données dans le panelModifierProspect

			PanelModifierSorties panModifiersorti = new PanelModifierSorties(
					boutonAnnulerAjoutSortie);
			sortieToModif = sortieMetier
					.findSortieByReference(referenceRecuperer);
			Date dateexat = sortieToModif.getSortDateEcheance();
			panModifiersorti.setReferenceCorrespondant(referenceRecuperer);
			Date dateexatement = sortieToModif.getSortDate();
			String usercreat = sortieToModif.getSortUserCreate();
			panModifiersorti.getTextNumero().setText(numeroRecuperer);
			panModifiersorti.getTextReference().setText(referenceRecuperer);
			panModifiersorti.getTextMontant().setText(montantrecuperer + "");
			// panModifiersorti.getComboxEtat().getSelectedItem()
			panModifiersorti.getDateCreation().setDate(dateCreationrecuperer);
			panModifiersorti.getDateEcheance().setDate(dateexat);
			panModifiersorti.getDateExate().setDate(dateexatement);
			panModifiersorti.getTextResponsable().setText(usercreat);
			boutonAjouter.setVisible(false);
			boutonSupprimer.setVisible(false);
			boutonImprimer.setVisible(false);
			boutonModifier.setVisible(false);
			boutonRechercher.setVisible(false);
			textRechercher.setVisible(false);
			labelDateDebut.setVisible(false);
			labelDateFin.setVisible(false);
			dateDebutProjet.setVisible(false);
			dateFinProjet.setVisible(false);
            boutonDetails.setVisible(false);
			panelbas.removeAll();
			panelbas.add(panModifiersorti, BorderLayout.SOUTH);
			validate();

		}
	}

	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
	if(	getTableListeSorties().getRowSelectionAllowed()==true){
		boutonSupprimer.setEnabled(true);
		boutonModifier.setEnabled(true);
	}
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
